/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wdm.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import org.wdm.beans.Bus;
import org.wdm.beans.BusStop;
import org.wdm.beans.points.Point;

/**
 *
 * @author thanasis
 */
public class BusUtils {
    public static boolean busStopsContainedInGoingDirection(Bus bus, BusStop first, BusStop second){
        if(BusUtils.isStartBeforeEnd(bus.getReturning(), first, second)){
            return false;
        }
        if(BusUtils.isStartBeforeEnd(bus.getGoing(), first, second)){
            return true;
        }
        return false;
    }
    
    public static Bus containsAtLeastOneBus(BusStop busStop, BusStop endBusStop) {
        for(Bus bus : busStop.getBusesList()){
            if(endBusStop.getBusesList().contains(bus)){
                if(BusUtils.isStartBeforeEnd(bus.getReturning(), busStop, endBusStop)){
                    return bus;
                }
                if(BusUtils.isStartBeforeEnd(bus.getGoing(), busStop, endBusStop)){
                    return bus;
                }
            }
        }
        return null;
    }
    
    public static TreeMap<Double,BusStop> getCloserBusStop(Point point, HashMap<String,BusStop> busStopMap){
        TreeMap<Double,BusStop> closerBusStops = new TreeMap<Double,BusStop>();
        Iterator<String> bsIterator = busStopMap.keySet().iterator();
        while(bsIterator.hasNext()){
            String naptan = bsIterator.next();
            BusStop busStop = busStopMap.get(naptan);
            double distance = busStop.getDistance(point, "M");
            closerBusStops.put(distance, busStop);
        }
        return closerBusStops;
    }
    
    
    public static boolean isStartBeforeEnd(List<BusStop> busStopList, BusStop start, BusStop end){
        if(!busStopList.contains(start) || !busStopList.contains(end)){
            return false;
        }
        int startIndex = busStopList.indexOf(start);
        int endIndex = busStopList.indexOf(end);
        return startIndex <= endIndex;
    }
    
    public static double getRouteDistance(List<BusStop> busStopList, BusStop start, BusStop end, String unit){
        if(busStopList.contains(start) && busStopList.contains(end)){
            double distance = 0.0;
            int startIndex = busStopList.indexOf(start);
            int endIndex = busStopList.indexOf(end);
            if(startIndex > endIndex){
                return -2;
            }
            for(int i = startIndex; i < endIndex-1; i++){
                distance += busStopList.get(i).getDistance(busStopList.get(i+1).getPoint(), unit);
            }
            return distance;
        } else {
            return -1;
        }
    }
    
    public static double getRouteDistance(List<BusStop> busStopList, String unit){
        double distance = 0.0;
        for(int i = 0; i < busStopList.size()-1; i++){
            distance += busStopList.get(i).getDistance(busStopList.get(i+1).getPoint(), unit);
        }
        return distance;
    }
}
